package com.yuan;

import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.joda.time.DateTime;
import org.junit.Test;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @Auther: yjw
 * @Date: 2020/04/23/11:03
 * @Description:
 */
public class ExcelWriteTest {
    String PATH = "G:\\狂神课程视频\\8番外\\POI\\POI联系代码\\yuan-poi";

    @Test
    public void testWrite03() throws IOException {
        Workbook workbook = new HSSFWorkbook();
        Sheet sheet1 = workbook.createSheet("袁佳伟联系代码");
        Row row1 = sheet1.createRow(0);

        Cell cell11 = row1.createCell(0);
        cell11.setCellValue("学习时间");
        Cell cell12 = row1.createCell(1);
        cell12.setCellValue("五个小时");

        Row row2 = sheet1.createRow(1);

        Cell cell21 = row2.createCell(0);
        cell21.setCellValue("代码时间");
        Cell cell22 = row2.createCell(1);
        String time = new DateTime().toString("yyyy-MM-dd HH:mm:ss");
        cell22.setCellValue(time);

        FileOutputStream stream = new FileOutputStream(PATH + "袁佳伟学习.xls");
        workbook.write(stream);
        stream.close();
    }
    @Test
    public void testWrite07() throws IOException {
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet1 = workbook.createSheet("袁佳伟联系代码");
        Row row1 = sheet1.createRow(0);

        Cell cell11 = row1.createCell(0);
        cell11.setCellValue("学习时间");
        Cell cell12 = row1.createCell(1);
        cell12.setCellValue("五个小时");

        Row row2 = sheet1.createRow(1);

        Cell cell21 = row2.createCell(0);
        cell21.setCellValue("代码时间");
        Cell cell22 = row2.createCell(1);
        String time = new DateTime().toString("yyyy-MM-dd HH:mm:ss");
        cell22.setCellValue(time);

        FileOutputStream stream = new FileOutputStream(PATH + "袁佳伟学习07.xlsx");
        workbook.write(stream);
        stream.close();
    }
    @Test
    public void bigData03() throws IOException {
        Workbook workbook = new HSSFWorkbook();
        Long begin = System.currentTimeMillis();
        Sheet sheet = workbook.createSheet("大数据");
        for (int rownumb = 0; rownumb < 65537; rownumb++){
            Row row = sheet.createRow(rownumb);
            for (int cell = 0; cell < 10; cell++){
                Cell rowCell = row.createCell(cell);
                rowCell.setCellValue(cell);
            }
        }

        FileOutputStream outputStream = new FileOutputStream(PATH + "大数据03.xls");
        workbook.write(outputStream);
        outputStream.close();
        Long finish = System.currentTimeMillis();
        System.out.println((double) (finish - begin)/1000);
    }
    @Test
    public void bigData07() throws IOException {
        Workbook workbook = new XSSFWorkbook();
        Long begin = System.currentTimeMillis();
        Sheet sheet = workbook.createSheet("大数据");
        for (int rownumb = 0; rownumb < 100000; rownumb++){
            Row row = sheet.createRow(rownumb);
            for (int cell = 0; cell < 10; cell++){
                Cell rowCell = row.createCell(cell);
                rowCell.setCellValue(cell);
            }
        }

        FileOutputStream outputStream = new FileOutputStream(PATH + "大数据07.xlsx");
        workbook.write(outputStream);
        outputStream.close();
        Long finish = System.currentTimeMillis();
        System.out.println((double) (finish - begin)/1000);
    }
    @Test
    public void bigDatasuper07() throws IOException {
        Workbook workbook = new SXSSFWorkbook();
        Long begin = System.currentTimeMillis();
        Sheet sheet = workbook.createSheet("大数据");
        for (int rownumb = 0; rownumb < 100000; rownumb++){
            Row row = sheet.createRow(rownumb);
            for (int cell = 0; cell < 10; cell++){
                Cell rowCell = row.createCell(cell);
                rowCell.setCellValue(cell);
            }
        }

        FileOutputStream outputStream = new FileOutputStream(PATH + "大数据07super.xlsx");
        workbook.write(outputStream);
        outputStream.close();
        ((SXSSFWorkbook)workbook).dispose(); //关闭临时文件
        Long finish = System.currentTimeMillis();
        System.out.println((double) (finish - begin)/1000);
    }
}
